﻿using Phoenix.Framework;
using Phoenix.Tests.Controllers;
using Phoenix.Tests.Features;
using Xunit;

namespace Phoenix.Tests
{
    public class DefaultControllerLocatorTests
    {
        [Fact]
        public void finds_controllers_in_controller_folder()
        {
            // arrange
            var type = typeof(TestController);
            var locator = new DefaultControllerLocator(type.Assembly);

            // act
            var resolved = locator.Locate(new ControllerAction("TestController", null, null));

            // assert
            Assert.Equal(type, resolved);
        }

        [Fact]
        public void finds_controllers_when_controller_suffix_not_specified()
        {
            // arrange
            var type = typeof(TestController);
            var locator = new DefaultControllerLocator(type.Assembly);

            // act
            var resolved = locator.Locate(new ControllerAction("Test", null, null));

            // assert
            Assert.Equal(type, resolved);
        }

        [Fact]
        public void finds_controllers_in_features_folder()
        {
            // arrange
            var type = typeof(FeatureController);
            var locator = new DefaultControllerLocator(type.Assembly);

            // act
            var resolved = locator.Locate(new ControllerAction("Feature", null, null));

            // assert
            Assert.Equal(type, resolved);
        }

        [Fact]
        public void can_register_same_named_controller_in_different_assemblies()
        {
            // arrange
            var type = typeof(MainController);
            var otherType = typeof (HelloWorld.Controllers.MainController);

            // act /assert (verify doesn't throw)
            var locator = new DefaultControllerLocator(type.Assembly, otherType.Assembly);
            locator.Locate(new ControllerAction("MainController", null, "HelloWorld"));
        }

        [Fact]
        public void can_register_same_named_controller_in_different_assemblies_using_addassembly()
        {
            // arrange
            var type = typeof(MainController);
            var otherType = typeof(HelloWorld.Controllers.MainController);
            var locator = new DefaultControllerLocator();
            locator.AddAssemblyToScan(type.Assembly);
            locator.Locate(new ControllerAction("Main", null, "HelloWorld"));

            // act /assert (verify doesn't throw)
            locator.AddAssemblyToScan(otherType.Assembly);
        }
    }
}